<?php
/*
---------------------------------------------------------------------
- 功能描述: 管理后台登录、退出
- 文件名:sysController.php
- 版本: 1.0
- 原作者:wangpeng
- 编写日期:2011-11-8
- 运行环境:---
- 修改记录:2011-11-8,wangpeng,添加新的indexAction方法
---------------------------------------------------------------------
*/

class sysController extends Base
{

	/**
	* 构造函数
	*
	* @access public
	* @param
	* @return void
	*/
	function __construct(){}

	/**
	* 默认首页
	*
	* @access public
	* @param  void
	* @return void
	*/
	function indexAction()
	{
		if(!Admin::isAdminLogin())
		{
			Common::header("/login.html");
		}

		$this->smarty->display("admin/index.html");
	}

	/**
	* 左菜单
	*
	* @access public
	* @param  void
	* @return void
	*/
	public function leftmenuAction()
	{
		if(!Admin::isAdminLogin())
		{
			Common::header("/login.html");
		}
		require_once CONTROLLER_DIR.'admin/adminmenuConfig.php';

		//我有权限的菜单
		$mymenu = array();
		$acl = explode(",", parent::$admin['acl']);
		if($acl)
		{
			//非全局权限
			if($acl[0] != 'aclAll')
			{
				foreach($acl as $key => $val)
				{
					$tmp = explode("/", $val);
					$acl_contraller[] = $tmp[1].$tmp[2].$tmp[3];
				}
			}

			//对菜单应用规则，是否展示
			foreach($menu as $key1 => $val1)
			{
				foreach($val1['sub'] as $key2 => $val2)
				{
					foreach($val2['sub'] as $key3 => $val3)
					{
						$menuitem = str_replace("/", "", substr($val3['link'],0,strpos($val3['link'], '.html')));

						//全部权限 或 指定权限
						if($acl[0] == 'aclAll' || in_array($menuitem, $acl_contraller))
						{
							$key1 = isset($mymenu[0]) ? $key1 : 0 ;
							if(!$mymenu[$key1]['title'])
							{
								$mymenu[$key1]['title'] = $val1['title'];
							}
							if(!$mymenu[$key1]['sub'][$key2]['title'])
							{
								$mymenu[$key1]['sub'][$key2]['title'] = $val2['title'];
							}
							$mymenu[$key1]['sub'][$key2]['sub'][$key3] = $val3;
						}
					}$key++;
				}
				
			}
		}

		$this->smarty->assign("menu", $mymenu);

		$this->smarty->display("admin/leftmenu.html");
	}

	/**
	* 默认首页
	*
	* @access public
	* @param  void
	* @return void
	*/
	function indexbodyAction()
	{
		if(!Admin::isAdminLogin())
		{
			Common::header("/login.html");
		}

		$this->smarty->display("admin/indexbody.html");
	}

	/**
	* 登录
	*
	* @access public
	* @param  void
	* @return void
	*/
	function loginAction()
	{
		if($_POST['act'] == 'do')
		{
			$this->smarty->assign("username", $_POST['username']);

			//用户名
			if(!$_POST['username'])
			{
				$this->error("请填写用户名", '', 'admin/login.html');
			}
			//密码
			if(!$_POST['password'])
			{
				$this->error("请填写密码", '', 'admin/login.html');
			}
			//验证码
			// if(strtolower($_POST['validate']) != $_SESSION['dd_ckstr'].'.a')
			// {
			// 	$this->error("验证码错误", '', 'admin/login.html');
			// }
			$this->admin_managerModule->login($_POST['username'],$_POST['password']);

			Common::header("/");
		}

		$this->smarty->display("admin/login.html");
	}

	/**
	* 修改密码
	*
	* @access public
	* @param  void
	* @return void
	*/
	function repassAction()
	{
		if($_POST['act'] == 'do')
		{
			//旧密码
			if(!$_POST['oldpass'])
			{
				$this->error("请填写旧密码", '', 'admin/repass.html');
			}
			//密码
			if(!$_POST['newpass'])
			{
				$this->error("请填写新密码", '', 'admin/repass.html');
			}
			if(strlen($_POST['newpass']) < 6)
			{
				$this->error("新密码不能少于6位", '', 'admin/repass.html');
			}

			//密码
			if($_POST['newpass'] != $_POST['renewpass'])
			{
				$this->error("两次输入的新密码不一致", '', 'admin/repass.html');
			}

			$pass = $this->admin_managerModule->getPass($_SESSION['admin']['adminid']);

			if($pass != md5($_POST['oldpass']))
			{
				$this->error("旧密码错误", '', 'admin/repass.html');
			}

			$this->admin_managerModule->setpass($_SESSION['admin']['adminid'], md5($_POST['newpass']));

			$this->error("操作已成功");
		}

		$this->smarty->display("admin/repass.html");
	}

	/**
	* 退出
	*
	* @access public
	* @param  void
	* @return void
	*/
	function logoutAction()
	{
		$this->admin_managerModule->logout();
		Common::header("/login.html");
	}

	/**
	* 验证码
	*
	* @access public
	* @param  void
	* @return void
	*/
	function vdimgckAction()
	{
		//获取随机字符
		$rndstring = '';
		for($i=0; $i<4; $i++) $rndstring .= chr(mt_rand(65,90));

		//如果支持GD，则绘图
		if(function_exists("imagecreate"))
		{
			//Firefox部份情况会多次请求的问题，5秒内刷新页面将不改变session
			$ntime = time();
			if(empty($_SESSION['dd_ckstr_last']) || empty($_SESSION['dd_ckstr']) || ($ntime - $_SESSION['dd_ckstr_last'] > 5))
			{
				$_SESSION['dd_ckstr'] = strtolower($rndstring);
				$_SESSION['dd_ckstr_last'] = $ntime;
			}
			$rndstring = $_SESSION['dd_ckstr'];
			$rndcodelen = strlen($rndstring);

			//创建图片，并设置背景色
			$im = imagecreate(50,20);
			ImageColorAllocate($im, 255,255,255);

			//背景线
			$lineColor1 = ImageColorAllocate($im,240,220,180);
			$lineColor2 = ImageColorAllocate($im,250,250,170);
			for($j=3;$j<=16;$j=$j+3)
			{
				imageline($im,2,$j,48,$j,$lineColor1);
			}
			for($j=2;$j<52;$j=$j+(mt_rand(3,6)))
			{
				imageline($im,$j,2,$j-6,18,$lineColor2);
			}

			//画边框
			$bordercolor = ImageColorAllocate($im, 0x99,0x99,0x99);
			imagerectangle($im, 0, 0, 49, 19, $bordercolor);

			//输出文字
			$fontColor = ImageColorAllocate($im, 48,61,50);
			for($i=0;$i<$rndcodelen;$i++)
			{
				$bc = mt_rand(0,1);
				$rndstring[$i] = strtoupper($rndstring[$i]);
				imagestring($im, 5, $i*10+6, mt_rand(2,4), $rndstring[$i], $fontColor);
			}

			header("Pragma:no-cache\r\n");
			header("Cache-Control:no-cache\r\n");
			header("Expires:0\r\n");

			//输出特定类型的图片格式，优先级为 gif -> jpg ->png
			if(function_exists("imagejpeg"))
			{
				header("content-type:image/jpeg\r\n");
				imagejpeg($im);
			}
			else
			{
				header("content-type:image/png\r\n");
				imagepng($im);
			}
			ImageDestroy($im);
			exit();
		}
		else
		{
			//不支持GD，只输出字母 ABCD
			$_SESSION['dd_ckstr'] = "abcd";
			$_SESSION['dd_ckstr_last'] = '';
			header("content-type:image/jpeg\r\n");
			header("Pragma:no-cache\r\n");
			header("Cache-Control:no-cache\r\n");
			header("Expires:0\r\n");
			$fp = fopen("data/vdcode.jpg","r");
			//echo fread($fp,filesize("data/vdcode.jpg"));
			fclose($fp);
			exit();
		}
	}
}
?>